﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class ReceiptBLL
    {
        private static readonly ICarrierDA carrierDA = DataAccess.CreateCarrierDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomerDA customerDA = DataAccess.CreateCustomerDA();
        private List<string> dataBaseID;
        private static readonly IEdiLogDA edilogDA = DataAccess.CreateEdiLogDA();
        private static readonly IInvLotDA invLotDA = DataAccess.CreateInvLotDA();
        private static readonly IInvTransDA invTransDA = DataAccess.CreateInvTransDA();
        private static readonly ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
        private static readonly ILpnDA lpnDA = DataAccess.CreateLpnDA();
        private static readonly IManPowerResourceDA manpowerResourceDA = DataAccess.CreateManPowerResourceDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOrdersDA ordersDA = DataAccess.CreateOrdersDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private static readonly IPickDetailDA pickDetailDA = DataAccess.CreatePickDetailDA();
        private static readonly IPortDA portDA = DataAccess.CreatePortDA();
        private PubParasInfo pubParasInfo;
        private static readonly IPurchaseOrderDA purchaseOrderDA = DataAccess.CreatePurchaseOrderDA();
        private static readonly IPurchaseOrderDetailDA purchaseOrderDetailDA = DataAccess.CreatePurchaseOrderDetailDA();
        private static readonly IReceiptDA receiptDA = DataAccess.CreateReceiptDA();
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly IReceiptDetailLpnDA receiptDetailLpnDA = DataAccess.CreateReceiptDetailLpnDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private string strCurrentWhName;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();

        public ReceiptBLL()
        {
            this.pubParasInfo = null;
            this.strCurrentWhName = "";
        }

        public ReceiptBLL(string currentWH)
        {
            this.pubParasInfo = null;
            this.strCurrentWhName = "";
            this.strCurrentWhName = currentWH;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public ReceiptBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            this.strCurrentWhName = "";
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        private List<ReceiptDetailInfo> ChangePurchaseOrderDetail2ReceiptDetailInfo(string strReceiptID, List<PurchaseOrderDetailInfo> purchaseOrderDetailInfos, int lineID, string createdBy, string vendorID, string vendorName)
        {
            string receiptID = strReceiptID;
            int tempLineID = lineID;
            List<ReceiptDetailInfo> receiptDetailInfos = new List<ReceiptDetailInfo>();
            ReceiptDetailInfo receiptDetail = null;
            if ((purchaseOrderDetailInfos != null) && (purchaseOrderDetailInfos.Count > 0))
            {
                for (int i = 0; i < purchaseOrderDetailInfos.Count; i++)
                {
                    receiptDetail = new ReceiptDetailInfo {
                        ReceiptID = receiptID,
                        ReceiptLineID = tempLineID * 10,
                        OwnerID = purchaseOrderDetailInfos[i].OwnerID,
                        ExternReceiptID = purchaseOrderDetailInfos[i].ExternalPoID,
                        ExternLineID = purchaseOrderDetailInfos[i].ExternalLineID,
                        PoID = purchaseOrderDetailInfos[i].PoID,
                        PoLineID = purchaseOrderDetailInfos[i].LineID,
                        SkuID = purchaseOrderDetailInfos[i].SkuID,
                        AltSkuID = "",
                        LpnID = "",
                        ReceivedDate = DateTime.Now,
                        ExpectedQty = purchaseOrderDetailInfos[i].OrderedQty,
                        AdjustedQty = purchaseOrderDetailInfos[i].AdjustedQty,
                        RejectedQty = purchaseOrderDetailInfos[i].RejectedQty,
                        ReceivedQty = 0.0M,
                        UomID = purchaseOrderDetailInfos[i].UomID,
                        PackID = purchaseOrderDetailInfos[i].PackID,
                        ToLoc = "",
                        ToLpnID = "",
                        ConditionCode = "OK",
                        SkuDescr = purchaseOrderDetailInfos[i].SkuDesc,
                        Udf1 = purchaseOrderDetailInfos[i].Udf1,
                        Udf2 = purchaseOrderDetailInfos[i].Udf2,
                        Udf3 = purchaseOrderDetailInfos[i].Udf3,
                        Udf4 = purchaseOrderDetailInfos[i].Udf4,
                        Udf5 = purchaseOrderDetailInfos[i].Udf5,
                        Udf6 = purchaseOrderDetailInfos[i].Udf6,
                        Udf7 = purchaseOrderDetailInfos[i].Udf7,
                        Udf8 = purchaseOrderDetailInfos[i].Udf8,
                        Udf9 = purchaseOrderDetailInfos[i].Udf9,
                        Udf10 = purchaseOrderDetailInfos[i].Udf10,
                        Remark = purchaseOrderDetailInfos[i].Remark
                    };
                    ReceiptLotInfo lotAttribute = new ReceiptLotInfo {
                        LotAttr01 = "",
                        LotAttr02 = "",
                        LotAttr03 = ""
                    };
                    SysSettingInfo sysSettingInfo = sysSettingDA.GetSysSettingByID("AutomaticallyWithTheItemNo", this.dataBaseID[0], this.pubParasInfo);
                    if ((sysSettingInfo != null) && (sysSettingInfo.Value == "Y"))
                    {
                        SkuInfo skuinfo = skuDA.GetSkuByID(purchaseOrderDetailInfos[i].OwnerID, purchaseOrderDetailInfos[i].SkuID, this.dataBaseID[0]);
                        if (skuinfo != null)
                        {
                            lotAttribute.LotAttr03 = skuinfo.ItemNoID;
                        }
                    }
                    lotAttribute.LotAttr04 = "";
                    lotAttribute.LotAttr05 = "";
                    lotAttribute.LotAttr06 = "";
                    lotAttribute.LotAttr07 = "";
                    lotAttribute.LotAttr08 = "";
                    lotAttribute.LotAttr09 = "OK";
                    lotAttribute.LotAttr10 = "";
                    lotAttribute.ReceiptDate = DateTime.Now;
                    receiptDetail.ReceiptLot = lotAttribute;
                    receiptDetail.Status = ReceiptStatus.New;
                    receiptDetail.CreatedBy = createdBy;
                    receiptDetail.UpdatedBy = createdBy;
                    receiptDetail.VendorID = vendorID;
                    receiptDetail.VendorName = vendorName;
                    receiptDetail.Cube = float.Parse(purchaseOrderDetailInfos[i].TotalCube.ToString());
                    receiptDetail.GrossWgt = float.Parse(purchaseOrderDetailInfos[i].TotalGrossWeight.ToString());
                    receiptDetail.NetWgt = float.Parse(purchaseOrderDetailInfos[i].TotalNetWeight.ToString());
                    receiptDetail.UnitPrice = float.Parse(purchaseOrderDetailInfos[i].UnitPrice.ToString());
                    receiptDetail.ExtendedPrice = (float) purchaseOrderDetailInfos[i].TotalPrice;
                    receiptDetail.EffectiveDate = DateTime.Now;
                    receiptDetail.QcRequired = "N";
                    receiptDetailInfos.Add(receiptDetail);
                    tempLineID++;
                }
            }
            return receiptDetailInfos;
        }

        public string CheckExternalIDExist(string externalID)
        {
            if (receiptDA.GetReceiptByExternalID(externalID, this.dataBaseID[0], this.pubParasInfo) != null)
            {
                return "Y";
            }
            return "N";
        }

        public string CheckPoIsGeneratedASN(string PoID)
        {
            if (receiptDA.GetReceiptByPoID(PoID, this.dataBaseID[0], this.pubParasInfo) != null)
            {
                return "Y";
            }
            return "N";
        }

        public string CloseReceipt(List<string> receiptID)
        {
            int i;
            List<BillingCredenceInfo> billingCredenceListInfo;
            string result = string.Empty;
            string whLoginID = this.dataBaseID[0];
            BillingOrderBLL billingOrderBLL = new BillingOrderBLL(this.pubParasInfo);
            Dictionary<string, List<BillingCredenceInfo>> billingCredenceListObjects = new Dictionary<string, List<BillingCredenceInfo>>();
            for (i = 0; receiptID.Count > i; i++)
            {
                billingCredenceListInfo = billingOrderBLL.GetInboundBillingCredenceListInfo(receiptID[i]);
                billingCredenceListObjects.Add(receiptID[i], billingCredenceListInfo);
            }
            if ((receiptID != null) && (receiptID.Count > 0))
            {
                for (i = 0; i < receiptID.Count; i++)
                {
                    DataBase dataBase = new DataBase();
                    DbConnection conn = dataBase.connection;
                    conn.Open();
                    DbTransaction tran = conn.BeginTransaction();
                    try
                    {
                        string status = ReceiptStatus.Closed;
                        receiptDA.UpdateReceiptStatus(dataBase, tran, receiptID[i], status, whLoginID, this.pubParasInfo.UserID);
                        if (receiptDA.GetReceiptByID(dataBase, tran, receiptID[i], whLoginID, this.pubParasInfo).IsPalletized == "Y")
                        {
                            receiptDetailLpnDA.UpdateReceiptDetailLpnStatus(dataBase, tran, receiptID[i], status, this.pubParasInfo.UserID, whLoginID);
                        }
                        else
                        {
                            receiptDetailDA.UpdateReceiptDetailStatus(dataBase, tran, receiptID[i], status, this.pubParasInfo.UserID, whLoginID);
                        }
                        if (billingCredenceListObjects.ContainsKey(receiptID[i]))
                        {
                            billingCredenceListInfo = new List<BillingCredenceInfo>();
                            billingCredenceListObjects.TryGetValue(receiptID[i], out billingCredenceListInfo);
                            billingOrderBLL.GenerateBillingCredenceByCloseOrder(dataBase, tran, receiptID[i], "InBound", billingCredenceListInfo);
                        }
                        tran.Commit();
                        result = result + receiptID[i] + ",";
                    }
                    catch
                    {
                        tran.Rollback();
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return (string.IsNullOrEmpty(result) ? "" : result.Substring(0, result.Length - 1));
        }

        public string DeleteReceipt(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<ReceiptInfo> receiptInfos = new List<ReceiptInfo>();
                ReceiptInfo receiptInfo = null;
                foreach (string id in IDS)
                {
                    receiptInfo = receiptDA.GetReceiptByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    if (receiptInfo.IsPalletized == "Y")
                    {
                        receiptInfo.ReceiptDetailInfos = receiptDetailLpnDA.GetReceiptDetailLpnListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    }
                    else
                    {
                        receiptInfo.ReceiptDetailInfos = receiptDetailDA.GetReceiptDetailListByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    }
                    receiptInfos.Add(receiptInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                receiptDetailLpnDA.DeleteReceiptDetailLpn(dataBase, tran, IDS, this.dataBaseID[0]);
                receiptDetailDA.DeleteReceiptDetail(dataBase, tran, IDS, this.dataBaseID[0]);
                receiptDA.DeleteReceipt(dataBase, tran, IDS, this.dataBaseID[0]);
                lpnDA.DeleteLpnByDocID(dataBase, tran, IDS, LpnDocType.InBound, this.dataBaseID[0]);
                List<string> orderType = new List<string>();
                List<string> orderID = new List<string>();
                for (i = 0; i < IDS.Count; i++)
                {
                    orderType.Add("ASN");
                    orderID.Add(IDS[i]);
                }
                manpowerResourceDA.DeleteManPowerResource(dataBase, tran, orderID, orderType, this.dataBaseID[0]);
                for (i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<ReceiptInfo>(dataBase, tran, receiptInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "RECEIPT", this.dataBaseID[0]);
                    if ((receiptInfos[i].ReceiptDetailInfos != null) && (receiptInfos[i].ReceiptDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < receiptInfos[i].ReceiptDetailInfos.Count; j++)
                        {
                            if (receiptInfos[i].IsPalletized == "Y")
                            {
                                auditDataBLL.InsertAuditData<ReceiptDetailInfo>(dataBase, tran, receiptInfos[i].ReceiptDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "RECEIPT_DETAIL_LPN", this.dataBaseID[0]);
                            }
                            else
                            {
                                auditDataBLL.InsertAuditData<ReceiptDetailInfo>(dataBase, tran, receiptInfos[i].ReceiptDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "RECEIPT_DETAIL", this.dataBaseID[0]);
                            }
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return receiptDA.GetAllReceipt(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetOutReceiptByQueryList(ReceiptQueryEntity receiptQuery)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(receiptQuery.ReceiptID))
            {
                sqlWhere.Add(" R.RECEIPT_ID " + receiptQuery.ReceiptID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.OwnerID))
            {
                sqlWhere.Add(" R.OWNER_ID " + receiptQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.PoID))
            {
                sqlWhere.Add(" R.PO_ID " + receiptQuery.PoID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.ExternalReceiptID))
            {
                sqlWhere.Add(" R.EXTERNAL_RECEIPT_ID " + receiptQuery.ExternalReceiptID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.VendorID))
            {
                sqlWhere.Add(" R.VENDOR_ID " + receiptQuery.VendorID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.VendorName))
            {
                sqlWhere.Add(" R.VENDOR_NAME " + receiptQuery.VendorName);
            }
            if (!string.IsNullOrEmpty(receiptQuery.ReceiptDate))
            {
                sqlWhere.Add(receiptQuery.ExpectedReceiptDate.Replace("@ReceiptDate@", "R.RECEIPT_DATE"));
            }
            return receiptDA.GetOutReceiptByQueryList(sqlWhere, receiptQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public ReceiptInfo GetReceiptByReceiptID(string receiptID)
        {
            ReceiptInfo receiptinfo = receiptDA.GetReceiptByReceiptID(receiptID, this.dataBaseID[0], this.pubParasInfo);
            if (receiptinfo != null)
            {
                if (receiptinfo.IsPalletized != "Y")
                {
                    throw new Exception("单据：" + receiptID + "未码盘,无法进行入库称重!");
                }
                if (receiptinfo.Status.ToUpper() != "RECEIVEDCOMPLETE")
                {
                    throw new Exception("单据：" + receiptID + "没有收货完成,无法进行入库称重!");
                }
                receiptinfo = receiptDA.GetReceiptDetailLPNByReceiptID(receiptinfo, this.dataBaseID[0]);
                List<string> isWeightSku = new List<string>();
                List<ReceivingWeightInfo> receivingWeightInfoList = new List<ReceivingWeightInfo>();
                if (ownerDA.CheckSameWeightByStyle(receiptinfo.OwnerID, this.dataBaseID[0]))
                {
                    List<string> skuStyle = new List<string>();
                    foreach (ReceivingWeightInfo info in receiptinfo.ReceivingWeightInfos)
                    {
                        if (string.IsNullOrEmpty(info.SkuStyle))
                        {
                            if (!isWeightSku.Contains(info.SkuID))
                            {
                                receivingWeightInfoList.Add(info);
                                isWeightSku.Add(info.SkuID);
                            }
                        }
                        else if (!skuStyle.Contains(info.SkuStyle))
                        {
                            receivingWeightInfoList.Add(info);
                            skuStyle.Add(info.SkuStyle);
                            isWeightSku.Add(info.SkuID);
                        }
                    }
                }
                else
                {
                    List<string> skuID = new List<string>();
                    foreach (ReceivingWeightInfo info in receiptinfo.ReceivingWeightInfos)
                    {
                        if (!skuID.Contains(info.SkuID))
                        {
                            receivingWeightInfoList.Add(info);
                            skuID.Add(info.SkuID);
                            isWeightSku.Add(info.SkuID);
                        }
                    }
                }
                receiptinfo.IsWeight = isWeightSku.Count.ToString();
                receiptinfo.ReceivingWeightInfos = receivingWeightInfoList;
            }
            return receiptinfo;
        }

        public ReceiptInfo GetReceiptInfoByID(string recepitID, ReceiptDetailQueryEntity receiptDetailQueryEntity)
        {
            ReceiptInfo receiptInfo = receiptDA.GetReceiptByID(recepitID, this.dataBaseID[0], this.pubParasInfo);
            if ((receiptInfo != null) && !string.IsNullOrEmpty(receiptInfo.ReceiptID))
            {
                List<string> strWhere = new List<string>();
                DataSet ds = null;
                if (receiptInfo.IsPalletized == "Y")
                {
                    ds = receiptDetailLpnDA.GetReceiptDetailByPagerQueryList(strWhere, receiptDetailQueryEntity, this.dataBaseID[0], this.pubParasInfo);
                }
                else
                {
                    ds = receiptDetailDA.GetReceiptDetailByPagerQueryList(strWhere, receiptDetailQueryEntity, this.dataBaseID[0], this.pubParasInfo);
                }
                if (((ds != null) && (ds.Tables.Count > 2)) && ((ds.Tables[2] != null) && (ds.Tables[2].Rows.Count > 0)))
                {
                    InboundHelper inboundHelper = new InboundHelper();
                    Dictionary<string, PackInfo> packInfoList = new Dictionary<string, PackInfo> {  };
                   
                    if (receiptInfo.IsPalletized == "Y")
                    {
                        ds.Tables[1].Rows[0]["LineID"] = commonMethod.GetMaxLineID("RECEIPT_DETAIL_LPN", receiptDetailQueryEntity.ReceiptID, this.dataBaseID[0]);
                    }
                    else
                    {
                        ds.Tables[1].Rows[0]["LineID"] = commonMethod.GetMaxLineID("RECEIPT_DETAIL", receiptDetailQueryEntity.ReceiptID, this.dataBaseID[0]);
                    }
                    for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                    {
                        PackInfo packInfo = null;
                        if (packInfoList.ContainsKey(ds.Tables[2].Rows[i]["PACK_ID"].ToString()))
                        {
                            packInfo = packInfoList[ds.Tables[2].Rows[i]["PACK_ID"].ToString()];
                        }
                        else
                        {
                            packInfo = packDA.GetPackByID(ds.Tables[2].Rows[i]["PACK_ID"].ToString(), this.dataBaseID[0]);
                            packInfoList.Add(ds.Tables[2].Rows[i]["PACK_ID"].ToString(), packInfo);
                        }
                        ds.Tables[2].Rows[i]["RECEIVED_QTY"] = inboundHelper.ExchangeUom(packInfo, ds.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(ds.Tables[2].Rows[i]["RECEIVED_QTY"]), "R");
                        ds.Tables[2].Rows[i]["EXPECTED_QTY"] = inboundHelper.ExchangeUom(packInfo, ds.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(ds.Tables[2].Rows[i]["EXPECTED_QTY"]), "R");
                    }
                }
                receiptInfo.DataSetReceiptDetails = ds;
            }
            return receiptInfo;
        }

        public ReceiptInfo GetReceiptInfoByID4Exec(string recepitID)
        {
            ReceiptInfo receiptInfo = receiptDA.GetReceiptByID(recepitID, this.dataBaseID[0], this.pubParasInfo);
            if (receiptInfo != null)
            {
                int i;
                PackInfo packInfo;
                int j;
                if (receiptInfo.IsPalletized == "Y")
                {
                    receiptInfo.ReceiptDetailInfos = receiptDetailLpnDA.GetReceiptDetailLpnListByID(recepitID, this.dataBaseID[0], this.pubParasInfo);
                    receiptInfo.ReceiptHistoryInfos = receiptDetailLpnDA.GetReceiptHistoryInfoListByID(recepitID, this.dataBaseID[0]);
                }
                else
                {
                    receiptInfo.ReceiptDetailInfos = receiptDetailDA.GetReceiptDetailListByID(recepitID, this.dataBaseID[0], this.pubParasInfo);
                    receiptInfo.ReceiptHistoryInfos = receiptDetailDA.GetReceiptHistoryInfoListByID(recepitID, this.dataBaseID[0]);
                }
                receiptInfo.PutawayDetailInfos = receiptDetailDA.GetPutAwayDetailInfoListByID(recepitID, this.dataBaseID[0], this.pubParasInfo);
                InboundHelper inboundHelper = new InboundHelper();
                Dictionary<string, PackInfo> packInfoList = new Dictionary<string, PackInfo>();
                if ((receiptInfo.ReceiptDetailInfos != null) && (receiptInfo.ReceiptDetailInfos.Count > 0))
                {
                    for (i = 0; i < receiptInfo.ReceiptDetailInfos.Count; i++)
                    {
                        receiptInfo.TotalExpectedQty += receiptInfo.ReceiptDetailInfos[i].ExpectedQty;
                        receiptInfo.TotalReceivedQty += receiptInfo.ReceiptDetailInfos[i].ReceivedQty;
                        receiptInfo.TotalGrossWgt += receiptInfo.ReceiptDetailInfos[i].GrossWgt;
                        receiptInfo.TotalCube += receiptInfo.ReceiptDetailInfos[i].Cube;
                        packInfo = null;
                        if (packInfoList.ContainsKey(receiptInfo.ReceiptDetailInfos[i].PackID))
                        {
                            packInfo = packInfoList[receiptInfo.ReceiptDetailInfos[i].PackID];
                        }
                        else
                        {
                            packInfo = packDA.GetPackByID(receiptInfo.ReceiptDetailInfos[i].PackID, this.dataBaseID[0]);
                            packInfoList.Add(receiptInfo.ReceiptDetailInfos[i].PackID, packInfo);
                        }
                        receiptInfo.ReceiptDetailInfos[i].ReceivedQty = inboundHelper.ExchangeUom(packInfo, receiptInfo.ReceiptDetailInfos[i].UomID, receiptInfo.ReceiptDetailInfos[i].ReceivedQty, "R");
                        receiptInfo.ReceiptDetailInfos[i].ExpectedQty = inboundHelper.ExchangeUom(packInfo, receiptInfo.ReceiptDetailInfos[i].UomID, receiptInfo.ReceiptDetailInfos[i].ExpectedQty, "R");
                        if ((receiptInfo.PutawayDetailInfos != null) && (receiptInfo.PutawayDetailInfos.Count > 0))
                        {
                            j = 0;
                            while (j < receiptInfo.PutawayDetailInfos.Count)
                            {
                                if (receiptInfo.ReceiptDetailInfos[i].ReceiptLineID.ToString() == receiptInfo.PutawayDetailInfos[j].ReceiptLineID)
                                {
                                    receiptInfo.ReceiptDetailInfos[i].IsGenPutAway = "Y";
                                    goto Label_038D;
                                }
                                receiptInfo.ReceiptDetailInfos[i].IsGenPutAway = "N";
                                j++;
                            }
                        Label_038D:;
                        }
                        else
                        {
                            receiptInfo.ReceiptDetailInfos[i].IsGenPutAway = "N";
                        }
                    }
                }
                if ((receiptInfo.ReceiptHistoryInfos == null) || (receiptInfo.ReceiptHistoryInfos.Count <= 0))
                {
                    return receiptInfo;
                }
                for (i = 0; i < receiptInfo.ReceiptHistoryInfos.Count; i++)
                {
                    packInfo = null;
                    if (packInfoList.ContainsKey(receiptInfo.ReceiptHistoryInfos[i].PackID))
                    {
                        packInfo = packInfoList[receiptInfo.ReceiptHistoryInfos[i].PackID];
                    }
                    else
                    {
                        packInfo = packDA.GetPackByID(receiptInfo.ReceiptHistoryInfos[i].PackID, this.dataBaseID[0]);
                        packInfoList.Add(receiptInfo.ReceiptHistoryInfos[i].PackID, packInfo);
                    }
                    receiptInfo.ReceiptHistoryInfos[i].ReceivedQty = inboundHelper.ExchangeUom(packInfo, receiptInfo.ReceiptHistoryInfos[i].Uom, receiptInfo.ReceiptHistoryInfos[i].ReceivedQty, "R");
                    receiptInfo.ReceiptHistoryInfos[i].CompletedQty = inboundHelper.ExchangeUom(packInfo, receiptInfo.ReceiptHistoryInfos[i].Uom, receiptInfo.ReceiptHistoryInfos[i].CompletedQty, "R");
                    receiptInfo.ReceiptHistoryInfos[i].RemainQty = inboundHelper.ExchangeUom(packInfo, receiptInfo.ReceiptHistoryInfos[i].Uom, receiptInfo.ReceiptHistoryInfos[i].RemainQty, "R");
                    receiptInfo.ReceiptHistoryInfos[i].AllocatedQty = inboundHelper.ExchangeUom(packInfo, receiptInfo.ReceiptHistoryInfos[i].Uom, receiptInfo.ReceiptHistoryInfos[i].AllocatedQty, "R");
                    if ((receiptInfo.PutawayDetailInfos != null) && (receiptInfo.PutawayDetailInfos.Count > 0))
                    {
                        for (j = 0; j < receiptInfo.PutawayDetailInfos.Count; j++)
                        {
                            if (receiptInfo.ReceiptHistoryInfos[i].ReceiptLineID == receiptInfo.PutawayDetailInfos[j].ReceiptLineID)
                            {
                                receiptInfo.ReceiptHistoryInfos[i].IsGenPutAway = "Y";
                                break;
                            }
                            receiptInfo.ReceiptHistoryInfos[i].IsGenPutAway = "N";
                        }
                    }
                    else
                    {
                        receiptInfo.ReceiptHistoryInfos[i].IsGenPutAway = "N";
                    }
                }
            }
            return receiptInfo;
        }

        public ReceiptInfo GetReceiptInfoByID4Exec(string recepitID, ReceiptDetailQueryEntity receiptDetailQueryEntity, ReceiptHistoryQueryEntity receiptHistoryQueryEntity, PutawayDetailQueryEntity putawayDetailQueryEntity)
        {
            string whLoginID = this.dataBaseID[0];
            ReceiptInfo receiptInfo = receiptDA.GetReceiptByID(recepitID, whLoginID, this.pubParasInfo);
            if (receiptInfo != null)
            {
                int i;
                PackInfo packInfo;
                List<string> strWhere = new List<string>();
                DataSet dsReceiptDetail = null;
                DataSet dsReceiptHistory = null;
                DataSet dsPutawayDetail = null;
                if (receiptInfo.IsPalletized == "Y")
                {
                    dsReceiptDetail = receiptDetailLpnDA.GetReceiptDetailByPagerQueryList(strWhere, receiptDetailQueryEntity, whLoginID, this.pubParasInfo);
                    dsReceiptHistory = receiptDetailLpnDA.GetReceiptHistoryByPagerQueryList(strWhere, receiptHistoryQueryEntity, whLoginID, this.pubParasInfo);
                }
                else
                {
                    dsReceiptDetail = receiptDetailDA.GetReceiptDetailByPagerQueryList(strWhere, receiptDetailQueryEntity, whLoginID, this.pubParasInfo);
                    dsReceiptHistory = receiptDetailDA.GetReceiptHistoryByPagerQueryList(strWhere, receiptHistoryQueryEntity, whLoginID, this.pubParasInfo);
                }
                dsPutawayDetail = receiptDetailDA.GetPutAwayDetailByPagerQueryList(strWhere, putawayDetailQueryEntity, whLoginID, this.pubParasInfo);
                InboundHelper inboundHelper = new InboundHelper();
                Dictionary<string, PackInfo> packInfoList = new Dictionary<string, PackInfo>();
                if (((dsReceiptDetail != null) && (dsReceiptDetail.Tables.Count > 2)) && ((dsReceiptDetail.Tables[2] != null) && (dsReceiptDetail.Tables[2].Rows.Count > 0)))
                {
                    dsReceiptDetail.Tables[1].Columns.Add("LineID");
                    if (receiptInfo.IsPalletized == "Y")
                    {
                        dsReceiptDetail.Tables[1].Rows[0]["LineID"] = commonMethod.GetMaxLineID("RECEIPT_DETAIL_LPN", receiptDetailQueryEntity.ReceiptID, this.dataBaseID[0]);
                    }
                    else
                    {
                        dsReceiptDetail.Tables[1].Rows[0]["LineID"] = commonMethod.GetMaxLineID("RECEIPT_DETAIL", receiptDetailQueryEntity.ReceiptID, this.dataBaseID[0]);
                    }
                    i = 0;
                    while (i < dsReceiptDetail.Tables[2].Rows.Count)
                    {
                        packInfo = null;
                        if (packInfoList.ContainsKey(dsReceiptDetail.Tables[2].Rows[i]["PACK_ID"].ToString()))
                        {
                            packInfo = packInfoList[dsReceiptDetail.Tables[2].Rows[i]["PACK_ID"].ToString()];
                        }
                        else
                        {
                            packInfo = packDA.GetPackByID(dsReceiptDetail.Tables[2].Rows[i]["PACK_ID"].ToString(), this.dataBaseID[0]);
                            packInfoList.Add(dsReceiptDetail.Tables[2].Rows[i]["PACK_ID"].ToString(), packInfo);
                        }
                        dsReceiptDetail.Tables[2].Rows[i]["RECEIVED_QTY"] = inboundHelper.ExchangeUom(packInfo, dsReceiptDetail.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(dsReceiptDetail.Tables[2].Rows[i]["RECEIVED_QTY"]), "R");
                        dsReceiptDetail.Tables[2].Rows[i]["EXPECTED_QTY"] = inboundHelper.ExchangeUom(packInfo, dsReceiptDetail.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(dsReceiptDetail.Tables[2].Rows[i]["EXPECTED_QTY"]), "R");
                        i++;
                    }
                }
                if ((dsReceiptHistory != null) && (dsReceiptHistory.Tables.Count > 2))
                {
                    for (i = 0; i < dsReceiptHistory.Tables[2].Rows.Count; i++)
                    {
                        packInfo = null;
                        if (packInfoList.ContainsKey(dsReceiptHistory.Tables[2].Rows[i]["PACK_ID"].ToString()))
                        {
                            packInfo = packInfoList[dsReceiptHistory.Tables[2].Rows[i]["PACK_ID"].ToString()];
                        }
                        else
                        {
                            packInfo = packDA.GetPackByID(dsReceiptHistory.Tables[2].Rows[i]["PACK_ID"].ToString(), this.dataBaseID[0]);
                            packInfoList.Add(dsReceiptHistory.Tables[2].Rows[i]["PACK_ID"].ToString(), packInfo);
                        }
                        dsReceiptHistory.Tables[2].Rows[i]["RECEIVED_QTY"] = inboundHelper.ExchangeUom(packInfo, dsReceiptHistory.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(dsReceiptHistory.Tables[2].Rows[i]["RECEIVED_QTY"]), "R");
                        dsReceiptHistory.Tables[2].Rows[i]["COMPLETED_QTY"] = inboundHelper.ExchangeUom(packInfo, dsReceiptHistory.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(dsReceiptHistory.Tables[2].Rows[i]["COMPLETED_QTY"]), "R");
                        dsReceiptHistory.Tables[2].Rows[i]["REMAIN_QTY"] = inboundHelper.ExchangeUom(packInfo, dsReceiptHistory.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(dsReceiptHistory.Tables[2].Rows[i]["REMAIN_QTY"]), "R");
                        dsReceiptHistory.Tables[2].Rows[i]["ALLOCATED_QTY"] = inboundHelper.ExchangeUom(packInfo, dsReceiptHistory.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(dsReceiptHistory.Tables[2].Rows[i]["ALLOCATED_QTY"]), "R");
                    }
                }
                receiptInfo.DataSetPutawayDetails = dsPutawayDetail;
                receiptInfo.DataSetReceiptDetails = dsReceiptDetail;
                receiptInfo.DataSetReceiptHistorys = dsReceiptHistory;
            }
            return receiptInfo;
        }

        public DataSet GetReceiptInfoByQueryEntity(ReceiptQueryEntity receiptQueryEntity)
        {
            ReceiptQueryEntity receiptQuery = receiptQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(receiptQuery.ReceiptID))
            {
                sqlWhere.Add(" R.RECEIPT_ID " + receiptQuery.ReceiptID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.ReceiptType))
            {
                sqlWhere.Add(" R.RECEIPT_TYPE " + receiptQuery.ReceiptType);
            }
            if (!string.IsNullOrEmpty(receiptQuery.OwnerID))
            {
                sqlWhere.Add(" R.OWNER_ID " + receiptQuery.OwnerID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.PoID))
            {
                sqlWhere.Add(" R.PO_ID " + receiptQuery.PoID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.ExternalReceiptID))
            {
                sqlWhere.Add(" R.EXTERNAL_RECEIPT_ID " + receiptQuery.ExternalReceiptID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.VendorID))
            {
                sqlWhere.Add(" R.VENDOR_ID " + receiptQuery.VendorID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.VendorName))
            {
                sqlWhere.Add(" R.VENDOR_NAME " + receiptQuery.VendorName);
            }
            if (!string.IsNullOrEmpty(receiptQuery.ExpectedReceiptDate))
            {
                sqlWhere.Add(receiptQuery.ExpectedReceiptDate.Replace("@ExpectedReceiptDate@", "R.EXPECTED_RECEIPT_DATE"));
            }
            if (!string.IsNullOrEmpty(receiptQuery.LottableMatchRequired))
            {
                sqlWhere.Add(" R.LOTTABLE_MATCH_REQUIRED " + receiptQuery.LottableMatchRequired);
            }
            if (!string.IsNullOrEmpty(receiptQuery.IsActive))
            {
                sqlWhere.Add(" R.IS_ACTIVE " + receiptQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(receiptQuery.CarrierID))
            {
                sqlWhere.Add(" R.CARRIER_ID " + receiptQuery.CarrierID);
            }
            if (!string.IsNullOrEmpty(receiptQuery.Status))
            {
                sqlWhere.Add(" R.STATUS" + receiptQuery.Status);
            }
            if (!string.IsNullOrEmpty(receiptQuery.DiffQty))
            {
                sqlWhere.Add(" diff.DIFF_QTY " + receiptQuery.DiffQty);
            }
            if (!string.IsNullOrEmpty(receiptQuery.DeclarationsType))
            {
                sqlWhere.Add(" R.DECLARATIONS_TYPE = '" + receiptQuery.DeclarationsType + "'");
            }
            return receiptDA.GetReceiptByQueryList(sqlWhere, receiptQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public ReceiptInfo GetReceiptScanReceivingByID(string receiptID)
        {
            ReceiptInfo receiptinfo = receiptDA.GetReceiptByID(receiptID, this.dataBaseID[0], this.pubParasInfo);
            if (receiptinfo != null)
            {
                if (receiptinfo.IsPalletized != "Y")
                {
                    throw new Exception("单据：" + receiptID + "未码盘,无法进行扫描收货!");
                }
                receiptinfo.ReceiptDetailInfos = receiptDetailDA.GetReceiptDetailListByID(receiptID, this.dataBaseID[0], this.pubParasInfo);
                receiptinfo.ReceiptDetailLpnInfos = receiptDetailLpnDA.GetReceiptDetailLpnListByID(receiptID, this.dataBaseID[0], this.pubParasInfo);
            }
            return receiptinfo;
        }

        public ReceiptInfo ImporFormSo(string soID, List<string> lineID, string vreceiptID, string maxLineID, string type)
        {
            OrdersInfo orderinfo = ordersDA.GetOrdersByID(soID, this.dataBaseID[0], this.pubParasInfo);
            ReceiptInfo receiptInfo = new ReceiptInfo();
            string receiptID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if (string.IsNullOrEmpty(vreceiptID))
                {
                    receiptInfo.OwnerID = orderinfo.OwnerID;
                    receiptInfo.IsPalletized = "N";
                    receiptInfo.ReceiptType = ReceiptTypes.Return;
                    receiptInfo.Status = ReceiptStatus.New;
                    receiptInfo.ExpectedReceiptDate = DateTime.Now;
                    receiptInfo.ExternalReceiptID = orderinfo.OrderID;
                    receiptInfo.Rma = "";
                    receiptInfo.PackingSlipNumber = "";
                    receiptInfo.WarehouseRef = "";
                    receiptInfo.VendorID = orderinfo.ConsigneeID;
                    receiptInfo.VendorName = orderinfo.CCompany;
                    receiptInfo.VendorCity = orderinfo.CCity;
                    receiptInfo.VendorProv = orderinfo.CProv;
                    receiptInfo.VendorContact = orderinfo.CContact1;
                    receiptInfo.VendorAddress = orderinfo.CAddress;
                    receiptInfo.VendorFax = orderinfo.CFax1;
                    receiptInfo.VendorEmail = orderinfo.CEmail1;
                    receiptInfo.VendorPhone = orderinfo.CPhone1;
                    receiptInfo.VendorZip = orderinfo.CZip;
                    receiptInfo.VendorCountry = orderinfo.CCountry;
                    receiptInfo.VendorRef = "";
                    receiptInfo.CarrierID = orderinfo.CarrierID;
                    if (!string.IsNullOrEmpty(receiptInfo.CarrierID))
                    {
                        CarrierInfo carrierInfo = carrierDA.GetCarrierByID(receiptInfo.CarrierID, this.dataBaseID[0], this.pubParasInfo);
                        receiptInfo.CarrierName = carrierInfo.Company;
                        receiptInfo.CarrierPhone = carrierInfo.Phone1;
                        receiptInfo.CarrierProv = carrierInfo.State;
                        receiptInfo.CarrierRef = "";
                        receiptInfo.CarrierZip = carrierInfo.Zip;
                        receiptInfo.CarrierEmail = carrierInfo.Email1;
                        receiptInfo.CarrierFax = carrierInfo.Fax1;
                        receiptInfo.CarrierCountry = carrierInfo.Country;
                        receiptInfo.CarrierContact = carrierInfo.Contact1;
                        receiptInfo.CarrierCity = carrierInfo.City;
                        receiptInfo.CarrierAddress = carrierInfo.Address;
                    }
                    receiptInfo.VehicleNumber = "";
                    receiptInfo.SrcCountry = "";
                    receiptInfo.ContainerType = "";
                    receiptInfo.ContainerQty = 0M;
                    receiptInfo.TransportationMode = orderinfo.TransportationMode;
                    receiptInfo.DestCountry = "";
                    receiptInfo.TrailerNumber = orderinfo.TrailerNumber;
                    receiptInfo.PaymentTerm = orderinfo.PaymentTerm;
                    receiptInfo.PlaceOfLoading = "";
                    receiptInfo.TrailerOwner = orderinfo.TrailerOwner;
                    receiptInfo.IncoTerms = "CFR";
                    receiptInfo.PlaceOfDischarge = "";
                    receiptInfo.DriverName = orderinfo.DriverName;
                    receiptInfo.PlaceOfDelivery = "";
                    receiptInfo.ArrivalDate = DateTime.Now;
                    receiptInfo.CarrierRef = "";
                    receiptInfo.Udf1 = orderinfo.Udf1;
                    receiptInfo.Udf2 = orderinfo.Udf2;
                    receiptInfo.Udf3 = orderinfo.Udf3;
                    receiptInfo.Udf4 = orderinfo.Udf4;
                    receiptInfo.Udf5 = orderinfo.Udf5;
                    receiptInfo.Udf6 = orderinfo.Udf6;
                    receiptInfo.Udf7 = orderinfo.Udf7;
                    receiptInfo.Udf8 = orderinfo.Udf8;
                    receiptInfo.Udf9 = orderinfo.Udf9;
                    receiptInfo.Udf10 = orderinfo.Udf10;
                    receiptInfo.Remark = orderinfo.Remark;
                    receiptInfo.LottableMatchRequired = "";
                    receiptInfo.IsActive = "Y";
                    receiptInfo.CreatedBy = this.pubParasInfo.UserID;
                    receiptID = this.InsertReceipt(dataBase, tran, receiptInfo, this.dataBaseID[0]);
                    maxLineID = "10";
                }
                else
                {
                    receiptID = vreceiptID;
                }
                if (orderinfo != null)
                {
                    if (!string.IsNullOrEmpty(receiptID))
                    {
                        receiptInfo.ReceiptDetailInfos = new List<ReceiptDetailInfo>();
                        int iLineID = 0;
                        if (!string.IsNullOrEmpty(maxLineID))
                        {
                            iLineID = int.Parse(maxLineID);
                        }
                        for (int i = 0; lineID.Count > i; i++)
                        {
                            PickDetailInfo pickDetailInfo = pickDetailDA.GetPickDetailByID(dataBase, tran, lineID[i], this.dataBaseID[0], this.pubParasInfo);
                            OrderDetailInfo orderDetailInfo = orderDetailDA.GetOrderDetailByID(dataBase, tran, pickDetailInfo.OrderID, pickDetailInfo.OrderLineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                            ReceiptDetailInfo receiptDetailInfo = new ReceiptDetailInfo {
                                ReceiptID = receiptID,
                                ReceiptLineID = iLineID,
                                OwnerID = pickDetailInfo.OwnerID,
                                SkuID = pickDetailInfo.SkuID,
                                Type = type,
                                Status = ReceiptStatus.New,
                                Udf1 = orderDetailInfo.Udf2,
                                Udf2 = orderDetailInfo.Udf2,
                                Udf3 = orderDetailInfo.Udf3,
                                Udf4 = orderDetailInfo.Udf4,
                                Udf5 = orderDetailInfo.Udf5,
                                Udf6 = orderDetailInfo.Udf6,
                                Udf7 = orderDetailInfo.Udf7,
                                Udf8 = orderDetailInfo.Udf8,
                                Udf9 = orderDetailInfo.Udf9,
                                Udf10 = orderDetailInfo.Udf10,
                                ExpectedQty = pickDetailInfo.Qty,
                                PackID = pickDetailInfo.PackID,
                                UomID = pickDetailInfo.UomID,
                                IsPalletized = "N"
                            };
                            SkuInfo skuinfo = skuDA.GetSkuByID(dataBase, tran, orderDetailInfo.OwnerID, orderDetailInfo.SkuID, this.dataBaseID[0]);
                            receiptDetailInfo.GrossWgt = Convert.ToSingle((decimal) (Convert.ToDecimal(skuinfo.GrossWgt) * pickDetailInfo.Qty));
                            receiptDetailInfo.Cube = Convert.ToSingle((decimal) (Convert.ToDecimal(skuinfo.Cube) * pickDetailInfo.Qty));
                            receiptDetailInfo.AltSkuID = orderDetailInfo.AltSkuID;
                            receiptDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                            receiptDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                            receiptDetailInfo.EffectiveDate = DateTime.Now;
                            receiptDetailInfo.ExternReceiptID = pickDetailInfo.OrderID;
                            receiptDetailInfo.ExternLineID = pickDetailInfo.OrderLineID.ToString();
                            receiptDetailInfo.PoID = "";
                            receiptDetailInfo.PoLineID = 0;
                            receiptDetailInfo.LpnID = "";
                            receiptDetailInfo.TariffID = "";
                            receiptDetailInfo.AdjustedQty = 0.0M;
                            receiptDetailInfo.ReceivedQty = 0.0M;
                            receiptDetailInfo.RejectedQty = 0.0M;
                            receiptDetailInfo.ToLoc = "";
                            receiptDetailInfo.ConditionCode = "";
                            receiptDetailInfo.CaseCnt = 0.0M;
                            receiptDetailInfo.InnerPack = 0.0M;
                            receiptDetailInfo.Pallet = 0.0M;
                            receiptDetailInfo.OtherUnit1 = 0f;
                            receiptDetailInfo.OtherUnit2 = 0f;
                            receiptDetailInfo.QcRequired = "";
                            receiptDetailInfo.DeclarationQty = 0M;
                            InvLotInfo invLotInfo = invLotDA.GetInvLotByID(dataBase, tran, pickDetailInfo.Lot, this.dataBaseID[0]);
                            receiptDetailInfo.UnitPrice = invLotInfo.UnitPrice;
                            receiptDetailInfo.ExtendedPrice = invLotInfo.UnitPrice * Convert.ToSingle(pickDetailInfo.Qty);
                            LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(dataBase, tran, invLotInfo.Lot, this.dataBaseID[0]);
                            receiptDetailInfo.ToLot = pickDetailInfo.Lot;
                            receiptDetailInfo.ReceiptLot = new ReceiptLotInfo();
                            receiptDetailInfo.ReceiptLot.LotAttr01 = lotAttributeInfo.LotAttr01;
                            receiptDetailInfo.ReceiptLot.LotAttr02 = lotAttributeInfo.LotAttr02;
                            receiptDetailInfo.ReceiptLot.LotAttr03 = lotAttributeInfo.LotAttr03;
                            if (string.IsNullOrEmpty(receiptDetailInfo.ReceiptLot.LotAttr03))
                            {
                                SysSettingInfo sysSettingInfo = sysSettingDA.GetSysSettingByID("AutomaticallyWithTheItemNo", this.dataBaseID[0], this.pubParasInfo);
                                if (((sysSettingInfo != null) && (sysSettingInfo.Value == "Y")) && (skuinfo != null))
                                {
                                    receiptDetailInfo.ReceiptLot.LotAttr03 = skuinfo.ItemNoID;
                                }
                            }
                            receiptDetailInfo.ReceiptLot.LotAttr04 = lotAttributeInfo.LotAttr04;
                            receiptDetailInfo.ReceiptLot.LotAttr05 = lotAttributeInfo.LotAttr05;
                            receiptDetailInfo.ReceiptLot.LotAttr06 = lotAttributeInfo.LotAttr06;
                            receiptDetailInfo.ReceiptLot.LotAttr07 = lotAttributeInfo.LotAttr07;
                            receiptDetailInfo.ReceiptLot.LotAttr08 = lotAttributeInfo.LotAttr08;
                            receiptDetailInfo.ReceiptLot.LotAttr09 = lotAttributeInfo.LotAttr09.ToUpper();
                            receiptDetailInfo.ReceiptLot.LotAttr10 = lotAttributeInfo.LotAttr10;
                            receiptDetailInfo.ReceiptLot.ExternalLot = lotAttributeInfo.ExternalLot;
                            receiptDetailInfo.ReceiptLot.VendorID = lotAttributeInfo.VendorID;
                            receiptDetailInfo.ReceiptLot.ProduceDate = lotAttributeInfo.ProduceDate;
                            receiptDetailInfo.ReceiptLot.ExpiryDate = lotAttributeInfo.ExpiryDate;
                            DateTime CS00003 = new DateTime();
                            receiptDetailInfo.ReceiptLot.ReceiptDate = CS00003;
                            receiptDetailDA.InsertReceiptDetail(dataBase, tran, receiptDetailInfo, this.dataBaseID[0]);
                            iLineID += 10;
                        }
                    }
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (!string.IsNullOrEmpty(receiptID))
            {
                ReceiptDetailQueryEntity receiptDetailQueryEntity = new ReceiptDetailQueryEntity {
                    IsGetAll = false,
                    CurrentPage = "0",
                    PageSize = "100",
                    SortField = "RD.RECEIPT_ID",
                    SortDirection = "DESC",
                    IsPalletized = "N",
                    ReceiptID = receiptID
                };
                receiptInfo = this.GetReceiptInfoByID(receiptID, receiptDetailQueryEntity);
            }
            return receiptInfo;
        }

        public string ImportFormPo(string poID, string vreceiptID, string type, string createdBy)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            int lineID = 0;
            List<ReceiptDetailInfo> receiptDetailTemp = null;
            List<ReceiptDetailInfo> receiptDetailInfos = new List<ReceiptDetailInfo>();
            CarrierInfo carrierInfo = null;
            string receiptID = vreceiptID;
            try
            {
                ReceiptInfo receiptInfo;
                string vendorID;
                string vendoName;
                int i;
                if (receiptID == "")
                {
                    PurchaseOrderInfo purchaseOrder = null;
                    receiptInfo = new ReceiptInfo();
                    purchaseOrder = purchaseOrderDA.GetPurchaseOrderByID(poID, this.dataBaseID[0], this.pubParasInfo);
                    if (purchaseOrder == null)
                    {
                        throw new Exception(poID + " 不存在!");
                    }
                    if (!string.IsNullOrEmpty(purchaseOrder.Carrier))
                    {
                        carrierInfo = carrierDA.GetCarrierByID(purchaseOrder.Carrier, this.dataBaseID[0], this.pubParasInfo);
                    }
                    if (purchaseOrder != null)
                    {
                        purchaseOrder.PurchaseOrderDetailInfos = purchaseOrderDetailDA.GetPurchaseOrderDetailListByID(poID, this.dataBaseID[0], this.pubParasInfo);
                        lineID = 1;
                        receiptInfo.ReceiptType = type;
                        receiptInfo.OwnerID = purchaseOrder.OwnerID;
                        receiptInfo.PoID = purchaseOrder.PoID;
                        receiptInfo.ExternalReceiptID = purchaseOrder.ExternalPoID;
                        receiptInfo.ExternalReceiptId2 = purchaseOrder.ExternalPoId2;
                        if (purchaseOrder.RequestDeliveryDate != new DateTime())
                        {
                            receiptInfo.ExpectedReceiptDate = purchaseOrder.RequestDeliveryDate;
                        }
                        else
                        {
                            receiptInfo.ExpectedReceiptDate = DateTime.Now;
                        }
                        receiptInfo.Status = ReceiptStatus.New;
                        receiptInfo.LottableMatchRequired = "N";
                        receiptInfo.IsPalletized = "N";
                        receiptInfo.IsActive = "Y";
                        receiptInfo.CreatedBy = createdBy;
                        receiptInfo.UpdatedBy = createdBy;
                        receiptInfo.ArrivalDate = DateTime.Now;
                        receiptInfo.VendorID = purchaseOrder.VendorID;
                        receiptInfo.VendorName = purchaseOrder.VendorName;
                        receiptInfo.VendorCity = purchaseOrder.VendorCity;
                        receiptInfo.VendorProv = purchaseOrder.VendorProv;
                        receiptInfo.VendorContact = purchaseOrder.VendorContact;
                        receiptInfo.VendorFax = purchaseOrder.VendorFax;
                        receiptInfo.VendorEmail = purchaseOrder.VendorEmail;
                        receiptInfo.VendorPhone = purchaseOrder.VendorPhone;
                        receiptInfo.VendorZip = purchaseOrder.VendorZip;
                        receiptInfo.VendorCountry = purchaseOrder.VendorCountry;
                        receiptInfo.VendorRef = purchaseOrder.VendorRef;
                        receiptInfo.VendorAddress = purchaseOrder.VendorAddress;
                        receiptInfo.Udf1 = purchaseOrder.Udf1;
                        receiptInfo.Udf2 = purchaseOrder.Udf2;
                        receiptInfo.Udf3 = purchaseOrder.Udf3;
                        receiptInfo.Udf4 = purchaseOrder.Udf4;
                        receiptInfo.Udf5 = purchaseOrder.Udf5;
                        receiptInfo.Udf6 = purchaseOrder.Udf6;
                        receiptInfo.Udf7 = purchaseOrder.Udf7;
                        receiptInfo.Udf8 = purchaseOrder.Udf8;
                        receiptInfo.Udf9 = purchaseOrder.Udf9;
                        receiptInfo.Udf10 = purchaseOrder.Udf10;
                        receiptInfo.Remark = purchaseOrder.Remark;
                        receiptInfo.SrcCountry = purchaseOrder.SrcCountry;
                        receiptInfo.DestCountry = purchaseOrder.DestCountry;
                        receiptInfo.PaymentTerm = purchaseOrder.PaymentTerm;
                        receiptInfo.PlaceOfLoading = purchaseOrder.PlaceOfLoading;
                        receiptInfo.IncoTerms = purchaseOrder.IncoTerms;
                        receiptInfo.PlaceOfDischarge = purchaseOrder.PlaceOfDischarge;
                        receiptInfo.PlaceOfDelivery = purchaseOrder.PlaceOfDelivery;
                        receiptInfo.TransportationMode = purchaseOrder.TransMode;
                        receiptInfo.ShipName = purchaseOrder.Vessel;
                        receiptInfo.Voyage = purchaseOrder.Voyage;
                        if (carrierInfo != null)
                        {
                            receiptInfo.CarrierAddress = carrierInfo.Address;
                            receiptInfo.CarrierCity = carrierInfo.City;
                            receiptInfo.CarrierContact = carrierInfo.Contact1;
                            receiptInfo.CarrierCountry = carrierInfo.Country;
                            receiptInfo.CarrierEmail = carrierInfo.Email1;
                            receiptInfo.CarrierFax = carrierInfo.Fax1;
                            receiptInfo.CarrierID = carrierInfo.CarrierID;
                            receiptInfo.CarrierName = carrierInfo.Company;
                            receiptInfo.CarrierPhone = carrierInfo.Phone1;
                            receiptInfo.CarrierProv = carrierInfo.State;
                            receiptInfo.CarrierZip = carrierInfo.Zip;
                        }
                        receiptInfo.CoreReleaseID = "";
                        receiptInfo.IsDeclartion = "N";
                        vendorID = string.IsNullOrEmpty(receiptInfo.VendorID) ? "" : receiptInfo.VendorID;
                        vendoName = string.IsNullOrEmpty(receiptInfo.VendorName) ? "" : receiptInfo.VendorName;
                        receiptDetailTemp = this.ChangePurchaseOrderDetail2ReceiptDetailInfo(receiptID, purchaseOrder.PurchaseOrderDetailInfos, lineID, createdBy, vendorID, vendoName);
                        for (i = 0; i < receiptDetailTemp.Count; i++)
                        {
                            receiptDetailInfos.Add(receiptDetailTemp[i]);
                        }
                        receiptInfo.ReceiptDetailInfos = receiptDetailInfos;
                        if (!string.IsNullOrEmpty(receiptInfo.ReceiptID))
                        {
                            if (receiptDA.CheckReceiptIDUnique(dataBase, tran, receiptInfo.ReceiptID, this.dataBaseID[0]))
                            {
                                PublicMethod.GetInstance().ShowAlertMessage("F4200_007", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.ReceiptID });
                            }
                            else
                            {
                                receiptID = receiptInfo.ReceiptID;
                            }
                        }
                        else
                        {
                            receiptID = commonMethod.GenNextNumStr(dataBase, tran, "RECEIPT", this.dataBaseID[0]);
                        }
                        receiptInfo.ReceiptID = receiptID;
                        receiptDA.InsertReceipt(dataBase, tran, receiptInfo, this.dataBaseID[0]);
                        for (i = 0; receiptInfo.ReceiptDetailInfos.Count > i; i++)
                        {
                            receiptInfo.ReceiptDetailInfos[i].ReceiptID = receiptID;
                            receiptInfo.ReceiptDetailInfos[i].ReceiptLineID = (i + 1) * 10;
                            receiptDetailDA.InsertReceiptDetail(dataBase, tran, receiptInfo.ReceiptDetailInfos[i], this.dataBaseID[0]);
                        }
                    }
                }
                else
                {
                    receiptInfo = receiptDA.GetReceiptByID(vreceiptID, this.dataBaseID[0], this.pubParasInfo);
                    if (receiptInfo == null)
                    {
                        throw new Exception(poID + " 不存在!");
                    }
                    receiptInfo.ReceiptDetailInfos = receiptDetailDA.GetReceiptDetailListByID(vreceiptID, this.dataBaseID[0], this.pubParasInfo);
                    PurchaseOrderInfo purchaseOrderInfo = purchaseOrderDA.GetPurchaseOrderByID(poID, this.dataBaseID[0], this.pubParasInfo);
                    if (purchaseOrderInfo != null)
                    {
                        if (!string.IsNullOrEmpty(purchaseOrderInfo.Carrier))
                        {
                            carrierInfo = carrierDA.GetCarrierByID(purchaseOrderInfo.Carrier, this.dataBaseID[0], this.pubParasInfo);
                        }
                        purchaseOrderInfo.PurchaseOrderDetailInfos = purchaseOrderDetailDA.GetPurchaseOrderDetailListByID(poID, this.dataBaseID[0], this.pubParasInfo);
                    }
                    if (receiptInfo != null)
                    {
                        if (carrierInfo != null)
                        {
                            receiptInfo.CarrierAddress = carrierInfo.Address;
                            receiptInfo.CarrierCity = carrierInfo.City;
                            receiptInfo.CarrierContact = carrierInfo.Contact1;
                            receiptInfo.CarrierCountry = carrierInfo.Country;
                            receiptInfo.CarrierEmail = carrierInfo.Email1;
                            receiptInfo.CarrierFax = carrierInfo.Fax1;
                            receiptInfo.CarrierID = carrierInfo.CarrierID;
                            receiptInfo.CarrierName = carrierInfo.Company;
                            receiptInfo.CarrierPhone = carrierInfo.Phone1;
                            receiptInfo.CarrierProv = carrierInfo.State;
                            receiptInfo.CarrierZip = carrierInfo.Zip;
                        }
                        if (receiptInfo.ReceiptDetailInfos != null)
                        {
                            lineID = receiptInfo.ReceiptDetailInfos.Count + 1;
                        }
                        else
                        {
                            lineID = 1;
                        }
                        vendorID = string.IsNullOrEmpty(purchaseOrderInfo.VendorID) ? "" : purchaseOrderInfo.VendorID;
                        vendoName = string.IsNullOrEmpty(purchaseOrderInfo.VendorName) ? "" : purchaseOrderInfo.VendorName;
                        receiptDetailTemp = this.ChangePurchaseOrderDetail2ReceiptDetailInfo(receiptInfo.ReceiptID, purchaseOrderInfo.PurchaseOrderDetailInfos, lineID, purchaseOrderInfo.CreatedBy, vendorID, vendoName);
                        ReceiptDetailBLL receiptDetailBll = new ReceiptDetailBLL(this.pubParasInfo);
                        for (i = 0; i < receiptDetailTemp.Count; i++)
                        {
                            receiptInfo.ReceiptDetailInfos.Add(receiptDetailTemp[i]);
                            receiptDetailDA.InsertReceiptDetail(dataBase, tran, receiptDetailTemp[i], this.dataBaseID[0]);
                        }
                        string asnStatus = new InboundHelper().GetASNStatus(receiptInfo.ReceiptDetailInfos);
                        receiptInfo.Status = asnStatus;
                        receiptDA.UpdateReceipt(dataBase, tran, receiptInfo, this.dataBaseID[0]);
                        receiptID = receiptInfo.ReceiptID;
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return receiptID;
        }

        public string InsertReceipt(ReceiptInfo receiptInfo)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string receiptID = string.Empty;
            try
            {
                receiptInfo.IsPalletized = "N";
                receiptID = this.InsertReceipt(dataBase, tran, receiptInfo, this.dataBaseID[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return receiptID;
        }

        public string InsertReceipt(DataBase dataBase, DbTransaction tran, ReceiptInfo receiptInfo, string whLoginID)
        {
            string receiptID = string.Empty;
            try
            {
                PortInfo destPort;
                IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
                if (!ownerDA.CheckTradePartnerIDUnique(receiptInfo.OwnerID, whLoginID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F4200_004", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.OwnerID });
                }
                else if (!commonMethod.CheckAuthorizationOwnerByUserID(this.pubParasInfo.UserID, receiptInfo.OwnerID))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F4200_005", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.OwnerID });
                }
                if (!string.IsNullOrEmpty(receiptInfo.DestinationPort))
                {
                    destPort = portDA.GetPortByID(receiptInfo.DestinationPort, this.dataBaseID[0]);
                    if ((destPort == null) || (destPort.IsActive == "N"))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2140_004", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.DestinationPort });
                    }
                }
                if (!string.IsNullOrEmpty(receiptInfo.DeparturePort))
                {
                    destPort = portDA.GetPortByID(receiptInfo.DeparturePort, this.dataBaseID[0]);
                    if ((destPort == null) || (destPort.IsActive == "N"))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2140_005", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.DeparturePort });
                    }
                }
                if (!string.IsNullOrEmpty(receiptInfo.VendorID))
                {
                    if (string.Compare(receiptInfo.ReceiptType, "Return", true) == 0)
                    {
                        if (!customerDA.CheckCustomerIDUnique(dataBase, tran, receiptInfo.VendorID, whLoginID))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2120_001", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                        }
                        else if (!customerDA.CheckAuthorizationCustomerIDUserID(dataBase, tran, receiptInfo.VendorID, whLoginID, this.pubParasInfo))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2120_002", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                        }
                    }
                    else
                    {
                        IVendorDA vendorDA = DataAccess.CreateVendorDA();
                        if (!vendorDA.CheckVendorIDUnique(dataBase, tran, receiptInfo.VendorID, whLoginID))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F4200_006", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                        }
                        else if (!vendorDA.CheckAuthorizationVendorIDUserID(dataBase, tran, receiptInfo.VendorID, whLoginID, this.pubParasInfo))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2130_002", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                        }
                    }
                }
                if (!string.IsNullOrEmpty(receiptInfo.CarrierID))
                {
                    ICarrierDA CarrierDa = DataAccess.CreateCarrierDA();
                    if (!CarrierDa.CheckCarrierIDUnique(dataBase, tran, receiptInfo.CarrierID, whLoginID))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2140_001", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                    }
                    else if (!CarrierDa.CheckAuthorizationCarrierUserID(dataBase, tran, receiptInfo.CarrierID, whLoginID, this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2140_002", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                    }
                }
                if (!string.IsNullOrEmpty(receiptInfo.ReceiptID))
                {
                    if (receiptDA.CheckReceiptIDUnique(dataBase, tran, receiptInfo.ReceiptID, whLoginID))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F4200_007", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.ReceiptID });
                    }
                    else
                    {
                        receiptID = receiptInfo.ReceiptID;
                    }
                }
                else
                {
                    receiptID = commonMethod.GenNextNumStr(dataBase, tran, "RECEIPT", whLoginID);
                }
                TradePartnerInfo owner = ownerDA.GetTradePartnerByID(dataBase, tran, receiptInfo.OwnerID, whLoginID);
                if ((owner != null) && !string.IsNullOrEmpty(owner.OwnerID))
                {
                    if (!string.IsNullOrEmpty(owner.AsnAlphaPrefix))
                    {
                        receiptID = owner.AsnAlphaPrefix + receiptID;
                    }
                }
                else
                {
                    List<string> gInitLocalb = new List<string> {
                        receiptInfo.OwnerID
                    };
                    PublicMethod.GetInstance().ShowAlertMessage("F4200_008", this.pubParasInfo.UserLanguage, gInitLocalb);
                }
                receiptInfo.ReceiptID = receiptID;
                string strIsPalletized = receiptInfo.IsPalletized;
                receiptInfo.IsPalletized = "N";
                receiptInfo.UpdatedBy = receiptInfo.CreatedBy;
                receiptInfo.CreatedDate = DateTime.Now;
                receiptInfo.UpdatedDate = DateTime.Now;
                receiptDA.InsertReceipt(dataBase, tran, receiptInfo, whLoginID);
                if (receiptInfo.ManPowerResourceInfo != null)
                {
                    receiptInfo.ManPowerResourceInfo.OrderID = receiptInfo.ReceiptID;
                    receiptInfo.ManPowerResourceInfo.CreatedDate = DateTime.Now;
                    receiptInfo.ManPowerResourceInfo.UpdatedDate = DateTime.Now;
                    ManPowerResourceInfo manpowerResourceInfo = receiptInfo.ManPowerResourceInfo;
                    manpowerResourceDA.InsertManPowerResource(dataBase, tran, manpowerResourceInfo, this.dataBaseID[0]);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<ReceiptInfo>(dataBase, tran, null, receiptInfo, AuditAction.Insert, this.pubParasInfo.UserID, "RECEIPT", whLoginID);
                receiptID = receiptInfo.ReceiptID;
                if (receiptInfo.IsCopy == "Y")
                {
                    if (strIsPalletized == "Y")
                    {
                        receiptDetailLpnDA.IsCopyReceiptDetailLpn(dataBase, tran, receiptInfo.ReceiptID, receiptInfo.OldReceiptID, this.pubParasInfo.UserID, whLoginID);
                        return receiptID;
                    }
                    receiptDetailDA.IsCopyReceiptDetail(dataBase, tran, receiptInfo.ReceiptID, receiptInfo.OldReceiptID, this.pubParasInfo.UserID, whLoginID);
                    return receiptID;
                }
                if (!(receiptInfo.IsTransfer == "Y"))
                {
                    return receiptID;
                }
                if ((receiptInfo.ReceiptDetailInfos == null) || (receiptInfo.ReceiptDetailInfos.Count <= 0))
                {
                    return receiptID;
                }
                InboundHelper inboundHelper = new InboundHelper();
                for (int i = 0; i < receiptInfo.ReceiptDetailInfos.Count; i++)
                {
                    receiptInfo.ReceiptDetailInfos[i].ReceiptID = receiptID;
                    if (receiptDetailDA.CheckReceiptDetailIDUnique(dataBase, tran, receiptInfo.ReceiptDetailInfos[i].ReceiptID, receiptInfo.ReceiptDetailInfos[i].ReceiptLineID.ToString(), whLoginID))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F4200_009", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.ReceiptDetailInfos[i].ReceiptID, receiptInfo.ReceiptDetailInfos[i].ReceiptLineID.ToString() });
                    }
                    else
                    {
                        receiptInfo.ReceiptDetailInfos[i].ExpectedQty = inboundHelper.ExchangeUom(dataBase, tran, receiptInfo.ReceiptDetailInfos[i].PackID, receiptInfo.ReceiptDetailInfos[i].UomID, receiptInfo.ReceiptDetailInfos[i].ExpectedQty, "W", whLoginID);
                        receiptInfo.ReceiptDetailInfos[i].CreatedDate = DateTime.Now;
                        receiptInfo.ReceiptDetailInfos[i].UpdatedDate = DateTime.Now;
                        receiptInfo.ReceiptDetailInfos[i].CreatedBy = this.pubParasInfo.UserID;
                        receiptInfo.ReceiptDetailInfos[i].UpdatedBy = this.pubParasInfo.UserID;
                        receiptDetailDA.InsertReceiptDetail(dataBase, tran, receiptInfo.ReceiptDetailInfos[i], whLoginID);
                        auditDataBLL.InsertAuditData<ReceiptDetailInfo>(dataBase, tran, null, receiptInfo.ReceiptDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "RECEIPT_DETAIL", whLoginID);
                    }
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return receiptID;
        }

        public string ReceiptRevert(AdjustmentInfo adjustmentInfo)
        {
            string strReceiptID = string.Empty;
            string whLoginID = this.dataBaseID[0];
            InboundHelper inbound = new InboundHelper();
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                if ((adjustmentInfo != null) && (adjustmentInfo.AdjustmentDetail.Count > 0))
                {
                    ReceiptInfo receiptInfo = receiptDA.GetReceiptByID(dataBase, tran, adjustmentInfo.AdjustmentDetail[0].ReceiptID, whLoginID, this.pubParasInfo);
                    if (receiptInfo != null)
                    {
                        strReceiptID = receiptInfo.ReceiptID;
                        IAdjustmentDA adjustment = DataAccess.CreateAdjustmentDA();
                        adjustmentInfo.AdjustmentID = commonMethod.GenNextNumStr(dataBase, tran, "ADJUSTMENT", whLoginID);
                        adjustmentInfo.Remark = "";
                        adjustment.InsertAdjustment(dataBase, tran, adjustmentInfo, whLoginID);
                        foreach (AdjustmentDetailInfo adjustmentdetail in adjustmentInfo.AdjustmentDetail)
                        {
                            InvTransInfo invTransInfo = invTransDA.GetInvTransByID(dataBase, tran, adjustmentdetail.TransID, whLoginID, this.pubParasInfo);
                            ReceiptDetailInfo receiptDetailInfo = receiptDetailDA.GetReceiptDetailByID(dataBase, tran, invTransInfo.ReceiptID, invTransInfo.ReceiptLineID.ToString(), whLoginID, this.pubParasInfo);
                            invTransInfo.UpdatedBy = this.pubParasInfo.UserID;
                            inbound.UpdateInvInRevertReceiving(dataBase, tran, invTransInfo, whLoginID);
                            inbound.UpdateTransStatus(dataBase, tran, invTransInfo.TransID, this.pubParasInfo.UserID, TransStatus.Cancelled, whLoginID);
                            decimal origReceivedQty = receiptDetailInfo.ReceivedQty;
                            receiptDetailInfo.ReceivedQty = invTransInfo.Qty * -1M;
                            receiptDetailInfo.FromLoc = receiptDetailInfo.ToLoc;
                            receiptDetailInfo.FromLpnID = receiptDetailInfo.ToLpnID;
                            receiptDetailInfo.ToLpnID = "";
                            receiptDetailInfo.Cube = receiptDetailInfo.Cube;
                            receiptDetailInfo.GrossWgt = receiptDetailInfo.GrossWgt;
                            receiptDetailInfo.NetWgt = receiptDetailInfo.NetWgt;
                            ReceiptLotInfo receiptLotInfo = new ReceiptLotInfo {
                                LotAttr01 = invTransInfo.LotAttr01,
                                LotAttr02 = invTransInfo.LotAttr02,
                                LotAttr03 = invTransInfo.LotAttr03,
                                LotAttr04 = invTransInfo.LotAttr04,
                                LotAttr05 = invTransInfo.LotAttr05,
                                LotAttr06 = invTransInfo.LotAttr06,
                                LotAttr07 = invTransInfo.LotAttr07,
                                LotAttr08 = invTransInfo.LotAttr08,
                                LotAttr09 = invTransInfo.LotAttr09,
                                LotAttr10 = invTransInfo.LotAttr10,
                                ProduceDate = invTransInfo.ProduceDate,
                                ExpiryDate = invTransInfo.ExpiryDate,
                                ExternalLot = invTransInfo.ExternalLot,
                                VendorID = invTransInfo.VendorID,
                                ReceiptDate = invTransInfo.ReceiptDate
                            };
                            receiptDetailInfo.ReceiptLot = receiptLotInfo;
                            string transID = inbound.InsertInvTrans(dataBase, tran, receiptDetailInfo, TransType.Adjustment, TransSourceType.ReceiptReversal, TransStatus.OK, receiptDetailInfo.ReceiptID, receiptDetailInfo.ReceiptLineID, whLoginID);
                            if (!string.IsNullOrEmpty(receiptDetailInfo.PoID))
                            {
                                new InboundHelper().UpdatePoQtyStatus(dataBase, tran, receiptDetailInfo, "subtract", whLoginID, this.pubParasInfo);
                            }
                            receiptDetailInfo.ToLot = "";
                            receiptDetailInfo.FromLoc = "";
                            receiptDetailInfo.FromLpnID = "";
                            receiptDetailInfo.ReceivedQty = origReceivedQty - invTransInfo.Qty;
                            receiptDetailInfo.Status = inbound.GetASNDetailStatus(receiptDetailInfo);
                            if (receiptInfo.IsPalletized != "Y")
                            {
                                receiptDetailDA.UpdateReceiptDetailStatus4Receiving(dataBase, tran, receiptDetailInfo, whLoginID);
                            }
                            else
                            {
                                receiptDetailLpnDA.UpdateReceiptDetailStatus4Receiving(dataBase, tran, receiptDetailInfo, whLoginID);
                            }
                            adjustmentdetail.CreatedBy = adjustmentInfo.CreatedBy;
                            adjustmentdetail.UpdatedBy = adjustmentInfo.UpdatedBy;
                            adjustmentdetail.TransID = transID;
                            adjustmentdetail.AdjustmentID = adjustmentInfo.AdjustmentID;
                            adjustmentdetail.ReasonCode = AdjReasonCode.UnReceive;
                            DataAccess.CreateAdjustmentDetailDA().InsertAdjustmentDetail(dataBase, tran, adjustmentdetail, whLoginID);
                        }
                        receiptInfo.Status = inbound.GetASNStatus(dataBase, tran, receiptInfo, whLoginID, this.pubParasInfo);
                        receiptDA.UpdateReceipt(dataBase, tran, receiptInfo, whLoginID);
                        tran.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (!string.IsNullOrEmpty(strReceiptID))
            {
                return adjustmentInfo.AdjustmentID;
            }
            return "";
        }

        public string UpdateReceipt(ReceiptInfo receiptInfo)
        {
            string receiptID = string.Empty;
            if (!string.IsNullOrEmpty(receiptInfo.VendorID))
            {
                if (string.Compare(receiptInfo.ReceiptType, "Return", true) == 0)
                {
                    if (!customerDA.CheckCustomerIDUnique(receiptInfo.VendorID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2120_001", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                    }
                    else if (!customerDA.CheckAuthorizationCustomerIDUserID(receiptInfo.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2120_002", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                    }
                }
                else
                {
                    IVendorDA vendorDA = DataAccess.CreateVendorDA();
                    if (!vendorDA.CheckVendorIDUnique(receiptInfo.VendorID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F4200_006", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                    }
                    else if (!vendorDA.CheckAuthorizationVendorIDUserID(receiptInfo.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2130_002", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                    }
                }
            }
            if (!string.IsNullOrEmpty(receiptInfo.CarrierID))
            {
                ICarrierDA CarrierDa = DataAccess.CreateCarrierDA();
                if (!CarrierDa.CheckCarrierIDUnique(receiptInfo.CarrierID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2140_001", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                }
                else if (!CarrierDa.CheckAuthorizationCarrierUserID(receiptInfo.CarrierID, this.dataBaseID[0], this.pubParasInfo))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2140_002", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.VendorID });
                }
            }
            if (!string.IsNullOrEmpty(receiptInfo.DestinationPort) && !portDA.CheckPortIDUnique(receiptInfo.DestinationPort, this.dataBaseID[0]))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F2140_004", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.DestinationPort });
            }
            if (!string.IsNullOrEmpty(receiptInfo.DeparturePort) && !portDA.CheckPortIDUnique(receiptInfo.DeparturePort, this.dataBaseID[0]))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F2140_005", this.pubParasInfo.UserLanguage, new List<string> { receiptInfo.DeparturePort });
            }
            try
            {
                ReceiptInfo oldReceiptInfo = receiptDA.GetReceiptByID(receiptInfo.ReceiptID, this.dataBaseID[0], this.pubParasInfo);
                if (receiptDA.UpdateReceipt(receiptInfo, this.dataBaseID[0]) <= 0)
                {
                    return receiptID;
                }
                if (receiptInfo.ManPowerResourceInfo != null)
                {
                    ManPowerResourceInfo manpowerResourceInfo = receiptInfo.ManPowerResourceInfo;
                    if (manpowerResourceDA.CheckManPowerResourceUnique(manpowerResourceInfo.OrderID, manpowerResourceInfo.OrderType, this.dataBaseID[0]))
                    {
                        manpowerResourceInfo.UpdatedDate = DateTime.Now;
                        manpowerResourceDA.UpdateManPowerResource(manpowerResourceInfo, this.dataBaseID[0]);
                    }
                    else
                    {
                        manpowerResourceInfo.OrderID = receiptInfo.ReceiptID;
                        manpowerResourceInfo.CreatedDate = DateTime.Now;
                        manpowerResourceInfo.UpdatedDate = DateTime.Now;
                        manpowerResourceDA.InsertManPowerResource(manpowerResourceInfo, this.dataBaseID[0]);
                    }
                }
                new AuditDataBLL().InsertAuditData<ReceiptInfo>(oldReceiptInfo, receiptInfo, AuditAction.Update, this.pubParasInfo.UserID, "RECEIPT", this.dataBaseID[0]);
                receiptID = receiptInfo.ReceiptID;
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return receiptID;
        }

        public string UpdateSkuWeight(string ownerID, List<string> skuID, List<string> weight, List<string> skuStyle)
        {
            List<string> skuGroupCount;
            int i;
            string str = "";
            if (ownerDA.CheckSameWeightByStyle(ownerID, this.dataBaseID[0]))
            {
                str = "货主" + ownerID + "|";
                skuGroupCount = new List<string>();
                for (i = 0; i < skuID.Count; i++)
                {
                    if (!skuGroupCount.Contains(skuID[i]) && (decimal.Parse(weight[i]) > 0M))
                    {
                        if (string.IsNullOrEmpty(skuStyle[i]))
                        {
                            if (skuDA.UpdateSkuWeight(skuID[i], ownerID, weight[i], this.dataBaseID[0], this.pubParasInfo) > 0)
                            {
                                str = str + "货品" + skuID[i] + "/";
                            }
                        }
                        else if (skuDA.UpdateSkuWeightSameStyle(ownerID, weight[i], skuStyle[i], this.dataBaseID[0], this.pubParasInfo) > 0)
                        {
                            str = str + "型号" + skuStyle[i] + "/";
                        }
                        skuGroupCount.Add(skuID[i]);
                    }
                }
            }
            else
            {
                str = "货主" + ownerID + "|";
                skuGroupCount = new List<string>();
                for (i = 0; i < skuID.Count; i++)
                {
                    if (!skuGroupCount.Contains(skuID[i]) && (decimal.Parse(weight[i]) > 0M))
                    {
                        if (skuDA.UpdateSkuWeight(skuID[i], ownerID, weight[i], this.dataBaseID[0], this.pubParasInfo) > 0)
                        {
                            str = str + "货品" + skuID[i] + "/";
                        }
                        skuGroupCount.Add(skuID[i]);
                    }
                }
            }
            if ((str == ("货主" + ownerID + "|型号")) || (str == ("货主" + ownerID + "|货品")))
            {
                throw new Exception("没有可更新数据!");
            }
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Substring(0, str.Length - 1);
            }
            return str;
        }
    }
}

